package ciyuan.Entity.MagicBall;

import ciyuan.Entity.EntityRegisterManager;
import ciyuan.Item.SkillBook.ThunderPossessed;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.DamagingProjectileEntity;
import net.minecraft.network.IPacket;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

public class MagicBallEntity extends DamagingProjectileEntity {

    public MagicBallEntity(LivingEntity entity, double powerX, double powerY, double powerZ, World world) {
        super(EntityRegisterManager.magicball.get(),entity,powerX,powerY,powerZ,world);
        this.setNoGravity(true);
    }

    public MagicBallEntity(EntityType<MagicBallEntity> entityEntityType, World world) {
        super(entityEntityType,world);
    }

    @Override
    public boolean isPickable() {
        return false;
    }

    @Override
    protected boolean shouldBurn() {
        return false;
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }



    @Override
    public void tick() {
        super.tick();
        if (this.level.isClientSide) {
            return;
        }
        if (this.getOwner()!=null) {
            if (this.distanceTo(this.getOwner())>20 && this.removed==false ) {
                SkillUtils.removeEntity(this);
            }
        }

    }

    @Override
    protected void onHitBlock(BlockRayTraceResult p_230299_1_) {
        SkillUtils.removeEntity(this);
    }

    @Override
    protected void onHitEntity(EntityRayTraceResult entityRayTraceResult) {
        if (this.level.isClientSide) {
            return;
        }
        if (!entityRayTraceResult.getEntity().isInvulnerable() && getOwner()!=null) {
            if (getOwner() instanceof PlayerEntity) {
                ThunderPossessed.onAttack(this.level,entityRayTraceResult.getEntity(),(PlayerEntity) getOwner());
            }
            entityRayTraceResult.getEntity().hurt(DamageSource.mobAttack((LivingEntity) getOwner()),10);
        }
        SkillUtils.removeEntity(this);
    }

    @Override
    public boolean hurt(DamageSource p_70097_1_, float p_70097_2_) {
        return false;
    }

    @Override
    protected boolean canHitEntity(Entity p_230298_1_) {
        return true;
    }
}
